<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Api extends CI_Controller {
	
	function __construct() {
        parent::__construct();
        $this->load->model('product_model');
        $this->load->model('review_model');
        $this->load->model('customer_model');
        $this->load->model('session_model');
        $this->load->model('category_model');
    }
    
	public function index(){

	}
	
	public function check_login()
	{
		echo 'check login';
	}
	
	public function get_product($product_id = NULL)
	{
		if (!isset($product_id)){
			$product_id = $this->input->post('product_id');
		}
		if (is_numeric($product_id)){
			$result = $this->product_model->get_product($product_id);
			foreach ($result as &$product){
				$images = explode(";", trim($product['product_images']));
				$product['product_images'] = null;
				for ($i = 0; $i < count($images); $i++){
					$product['product_images'][] = base_url('images')."/".$images[$i];
				}
				$product['base_url'] = base_url('images');
			}
			echo json_encode($result);					
		}else{
			echo json_encode("FALSE");
		}	
		die();	
	}
	
	public function get_product_array($product_ids_string)
	{
		if (!isset($product_id)){
			$product_id = $this->input->post('product_id');
		}
		if (is_numeric($product_id)){
			$result = $this->product_model->get_product($product_id);
			foreach ($result as &$product){
				$images = explode(";", trim($product['product_images']));
				$product['product_images'] = null;
				for ($i = 0; $i < count($images); $i++){
					$product['product_images'][] = base_url('images')."/".$images[$i];
				}
				$product['base_url'] = base_url('images');
			}
			echo json_encode($result);					
		}else{
			echo json_encode("FALSE");
		}	
		die();	
	}
	
	public function get_all_product(){
		$products = $this->product_model->get_all_product();
		foreach ($products as &$product){
			$images = explode(";", trim($product['product_images']));
			$product['product_images'] = null;
				for ($i = 0; $i < count($images); $i++){
					$product['product_images'][] = base_url('images')."/".$images[$i];
				}
			$product['base_url'] = base_url('images');
		}
		echo json_encode($products);
		die();
	}
	
	public function get_product_details($product_id = NULL){
		if (!isset($product_id)){
			$product_id = $this->input->post('product_id');
		}
		if (is_numeric($product_id)){
			$result = $this->product_model->get_product_details($product_id);
			$result = $this->edit_information_image($result);
			$result[0]['seller_review_number'] = $this->review_model->get_seller_review_number($result[0]['seller_id']);
			$result[0]['product_review_number'] = $this->review_model->get_product_review_number($product_id);
			echo json_encode($result);					
		}else{
			echo json_encode("FALSE");
		}	
		die();	
	}
	
	public function get_seller_review($seller_id = NULL, $offset = 0, $limit = 1){
		if (!isset($seller_id)){
			$product_id = $this->input->post('product_id');
		}
		if (is_numeric($seller_id)){
			$result = $this->review_model->get_seller_review($seller_id, $offset, $limit);
			echo json_encode($result);					
		}else{
			echo json_encode("FALSE");
		}	
		die();	
	}
	
	public function search_product($key = NULL, $offset = NULL, $limit = 4){
		if (!isset($key)){
			$key = $this->input->post('key_word');	
		}
		$offset = $this->input->post('offset');
		if ($offset == 0){
			$count = count($this->product_model->search_product($key));
		}
		$result = $this->product_model->search_product($key, $offset, $limit);
		$result = $this->edit_information_image($result);		
		foreach ($result as &$product){
			$product['seller_review_number'] = $this->review_model->get_seller_review_number($product['seller_id']);
			$product['product_review_number'] = $this->review_model->get_product_review_number($product['product_id']);
		}
		echo json_encode(array('productArray' => $result, 'count' => isset($count) ? $count : -1));
		die();
	}
	
	public function sign_in(){
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		if (isset($email) && isset($password)){
			$result = $this->customer_model->get_customer($email, $password);
		}
		if ($result){
			echo json_encode($result);
		}else{
			echo 'FALSE';
		}
		die();
	}

	public function sign_out(){
		$customer_id = $this->input->post('id');
		$viewed_product_id = $this->input->post('viewed_product_id');
		
	}
	
	public function register($email='email', $name = 'name', $password = 'password'){
		$name = $this->input->post('name');
		$email = $this->input->post('email');
		$password = $this->input->post('password');

		//Check input data
		
		//Insert to database
		$result = $this->customer_model->insert($name, $email, $password);
		if ($result){
			echo json_encode(array('status' => 'SUCCESS', 'id' => $result));
		}else{
			echo json_encode(array('status' => 'FAIL'));
		}
		die();
	}
	
	public function get_cart($user_id){
		$user_id = $this->input->post('userID');
		if (!empty($user_id)){
			$result = $this->session_model->get_cart($user_id);
			$cart = array();
			if ($result){
				$result = explode("|", trim($result[0]['cart']));
				for ($i = 0; $i < count($result); $i++){
					if (array_key_exists("'".$result[$i]."'", $cart)){
						$cart["'".$result[$i]."'"] += 1;
					}else{
						$cart["'".$result[$i]."'"] = 1;
					}
				}
				$cart['status'] = 'FILLED';
			}else{
				$cart['status'] = 'EMPTY';
			}
			echo json_encode($cart);
		}else{
			echo json_encode(array('status' => 'ERROR'));
		}
		die();
	}
	
	public function order(){
		
	}
	
	public function get_category_list(){
		$result = $this->category_model->get_category_list();
		echo json_encode($result);
		die();
	}
	
	public function get_product_by_category(){
		$category_id = $this->input->post('category_id');
		$offset = $this->input->post('offset');
		$result = $this->product_model->get_product_by_category($category_id, $offset);
		$result = $this->edit_information_image($result);
		echo json_encode($result);
		die();
	}
	
	private function edit_information_image($result){
		foreach ($result as &$product){
			$images = explode(";", trim($product['product_images']));
			$product['product_images'] = null;
			for ($i = 0; $i < count($images); $i++){
				$product['product_images'][] = base_url('images')."/".$images[$i];
			}
			$product['base_url'] = base_url('images');
		}
		return $result;
	}
}

/* End of file api.php */
/* Location: ./application/controllers/api.php */